<?php

namespace App\Models\Main;

use Illuminate\Support\Facades\Cache;

/**
 * This is the model class for table "ads".
 * @property int10unsigned id
 * @property tinyint1unsigned genre    广告位：1APP-首页，2APP-订单，
 * 3表示pc版首页   4表示pc版订单
 * @property varchar255 img    广告图片链接
 * @property varchar255 link    跳转链接，默认未空字符串
 * @property tinyint1unsigned del    0未删除，1已删除，
 * @property int10unsigned created_at    创建时间
 * @property int10unsigned updated_at    更新时间
 * @property int10unsigned paixu    从大到小的排序
 */
class AdModel extends BaseModel
{
    /**
     * 广告
     */

    protected $table = 'ads';
    protected $fillable = [
        'id', 'genre', 'img', 'link', 'del', 'created_at', 'updated_at',
    ];

    ///广告位：1APP-首页，2APP-订单，
    //3表示pc版首页   4表示pc版订单
    const genre_app_home = 1;///app版首页
    const genre_app_order = 2;///app版订单
    ///
    const genre_pc_home = 3;///pc版首页
    const genre_pc_order = 4;///pc版订单
    ///
    ///
    const genre_H5_home = 5;///H5版首页
    const genre_H5_order = 6;///H5版订单
    ///
    ///
    ///
    public static function getGenre($status = null)
    {
        $list = [
            self::genre_app_home => 'app版首页',
            self::genre_app_order => 'app版订单',
            self::genre_pc_home => 'pc版首页',
            self::genre_pc_order => 'pc版订单',

            self::genre_H5_home => 'H5版首页',
            self::genre_H5_order => 'H5版订单',

        ];
        if ($status === null) {
            return $list;
        }
        return isset($list[$status]) ? $list[$status] : '';
    }
    ///
    ///
    ///

    /**
     * genre 广告位：1APP-首页，2APP-订单，
     *              1-50代表APP端，51-100代表PC端，
     * img 广告图片地址
     * link 跳转链接，默认未空字符串
     * del 0未删除，1已删除
     */

    public function getGenres()
    {
        return array(
            1 => 'APP-首页', 'APP-订单',
        );
    }

    public function getGenreName()
    {
        $genre = $this->genre;
        $genres = $this->getGenres();
        return array_key_exists($genre, $genres) ?
            $genres[$genre] : '';
    }


    public function attributeLabels()
    {
        return [
            'id' => 'id',
            'genre' => '广告位',
            'img' => '广告图片链接',
            'link' => '跳转链接',
            'del' => '0未删除，1已删除，',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'paixu' => '从大到小的排序',
        ];
    }


    public static function getLunbotu($genre)
    {
        $list = \Illuminate\Support\Facades\Cache::remember('getLunbotu' . $genre, 10, function () use ($genre) {
            $list = self::where([
                'genre' => $genre,
                'del' => 0
            ])->orderBy('paixu', 'desc')
                ->select([
                    'img', 'link'
                ])
                ->take(5)->get()
                ->toArray();
            return $list;
        });
        return $list;
    }


}